﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Reflection;


namespace QHW.Comp.AppEnum
{
    /// <summary>
    /// 枚举 公共类 lzy 2011.6.20
    /// </summary>
    public class EnumHelper
    {
        /// <summary>
        /// 得到枚举的描述
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string GetEnumDescription(Enum value)
        {
            Type type = value.GetType();
            string objName = string.Format("Enum.{0}.{1}", type.Name, value);
            object obj = QHW.Comp.DataCache.GetCache(objName);
            if (obj == null)
            {
                MemberInfo[] memInfo = type.GetMember(value.ToString());
                if (memInfo.Length > 0)// if (memInfo != null && memInfo.Length > 0)
                {
                    object[] attrs = memInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false);

                    if (attrs.Length > 0)// if (attrs != null && attrs.Length > 0)
                    {
                        obj = ((DescriptionAttribute)attrs[0]).Description;
                    }
                }
                if (obj == null)
                    obj = value;
                QHW.Comp.DataCache.SetCache(objName, obj, 120, false);
            }
            return obj.ToString();
        }

        ///// <summary>
        ///// 获取枚举项数
        ///// </summary>
        ///// <param name="type">枚举类型</param>
        ///// <returns></returns>
        //public static int GetCount(Type type)
        //{
        //    return Enum.GetValues(type).Length;
        //}

        /// <summary>
        /// 得到包含值和名称的枚举集合
        /// </summary>
        /// <param name="type">枚举类型</param>
        /// <returns></returns>
        public static IList GetEnumTextList(Type type)
        {
            if (type == null) throw new ArgumentNullException("type");
            if (!type.IsEnum) throw new ArgumentException("Type provided must be an Enum.", "type");

            ArrayList list = new ArrayList();
            Array array = Enum.GetValues(type);
            foreach (Enum value in array)
            {
                list.Add(new KeyValuePair<int, string>(Convert.ToInt32(value), value.ToString()));
            }

            return list;
        }

        /// <summary>
        /// 得到枚举描述集合
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public static IList GetEnumDescriptionList(Type type)
        {
            if (type == null || !type.IsEnum) return null;
            ArrayList list = new ArrayList();
            Array array = Enum.GetValues(type);
            foreach (Enum value in array)
            {
                list.Add(new KeyValuePair<int, string>(Convert.ToInt32(value), GetEnumDescription(value)));
            }
            return list;
        }

        public static Dictionary<int, string> GetEnumDescriptionDicList(Type type)
        {
            if (type == null || !type.IsEnum) return null;
            Dictionary<int, string> list = new Dictionary<int, string>();
            Array array = Enum.GetValues(type);
            foreach (Enum value in array)
            {
                list.Add(Convert.ToInt32(value), GetEnumDescription(value));
            }
            return list;
        }

        /// <summary>
        /// 根据枚举的内容编号获取对应的描述信息
        /// </summary>
        /// <param name="type"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public static string GetDescriptionByValue(Type type, int value)
        {
            Dictionary<int, string> list = GetEnumDescriptionDicList(type);
            string des = string.Empty;
            foreach (KeyValuePair<int, string> item in list)
            {
                if (item.Key == value)
                {
                    des=item.Value;
                }
            }
            return des;
        }


        /// <summary>
        /// 根据枚举名称获取枚举值
        /// </summary>
        /// <param name="enumName">枚举名称</param>
        /// <param name="type">枚举</param>
        /// <returns></returns>
        public static int GetValueByEnumName(string enumName, Type type)
        {
             
            Array valueList = Enum.GetValues(type);
            Dictionary<string, int> list = new Dictionary<string, int>();
            for (int i = 0; i < valueList.Length; i++)
            {
                list.Add(valueList.GetValue(i).ToString(), (int)valueList.GetValue(i));
            }
            if (list.ContainsKey(enumName))
            {
                return list[enumName];
            }
            else {
                return -99999;
            }
        }
    }
}
